
package gui.panels;

import gui.MainFrame;
import gui.listeners.GuiActionEvent;
import gui.listeners.GuiActionEventData;
import gui.listeners.GuiActionEventType;
import gui.model.DrinkModel;
import gui.panels.components.MmiButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author JtR
 */
public class SelectFavoritePanel extends GuiPanel {
    private MmiButton cappuccinoButton, espressoButton, coffeeButton, teaButton;
    private JLabel coffeeText, teaText;
    private String inset = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    
    public SelectFavoritePanel(MainFrame parent) {
        super(parent, BackgroundType.BOTH);
    }
    
    @Override
    protected void initComponents() {
        JPanel coffeeWrapper = new JPanel(new GridBagLayout());
        coffeeWrapper.setBackground(Color.WHITE);
        coffeeWrapper.setBorder(new LineBorder(Color.BLACK));
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(145, 20, 0, 0);
        add(coffeeWrapper, constraints);
        
        JPanel teaWrapper = new JPanel(new GridBagLayout());
        teaWrapper.setBackground(Color.WHITE);
        teaWrapper.setBorder(new LineBorder(Color.BLACK));
        constraints.insets = new Insets(0, 0, 20, 20);
        constraints.anchor = GridBagConstraints.SOUTHEAST;
        add(teaWrapper, constraints);
        
        cappuccinoButton = new MmiButton("res/kaffeepause/Cappuchino.png", 300);
        cappuccinoButton.addActionListener(new CoffeeMaker());
        constraints.insets = new Insets(300, 200, 0, 0);
        constraints.anchor = GridBagConstraints.NORTHWEST;
        add(cappuccinoButton, constraints);
        
        espressoButton = new MmiButton("res/kaffeepause/espresso.png", 230);
        espressoButton.addActionListener(new CoffeeMaker());
        constraints.insets = new Insets(350, 200, 0, 0);
        add(espressoButton, constraints);
        
        coffeeButton = new MmiButton("res/kaffeepause/schwarzerKaffee.png", 300);
        coffeeButton.addActionListener(new CoffeeMaker());
        constraints.insets = new Insets(200, 200, 0, 0);
        add(coffeeButton, constraints);
        
        teaButton = new MmiButton("res/teetime/teeglas.png", 250);
        teaButton.addActionListener(new TeaMaker());
        constraints.insets = new Insets(250, 600, 0, 0);
        add(teaButton, constraints);
        
        coffeeText = new JLabel();
        teaText = new JLabel();
        coffeeText.setFont(coffeeText.getFont().deriveFont(18f).deriveFont(Font.PLAIN));
        teaText.setFont(teaText.getFont().deriveFont(18f).deriveFont(Font.PLAIN));
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.insets = new Insets(15, 15, 15, 15);
        coffeeWrapper.add(coffeeText, constraints);
        teaWrapper.add(teaText, constraints);
    }
    
    private class CoffeeMaker implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(JOptionPane.showConfirmDialog(SelectFavoritePanel.this, "Diesen Kaffee nun zubereiten?", "Bestätigung", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.MAKE_FAVORITE_COFFEE));
            }
        }
    }
    
    private class TeaMaker implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(JOptionPane.showConfirmDialog(SelectFavoritePanel.this, "Diesen Tee nun zubereiten?", "Bestätigung", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.MAKE_FAVORITE_TEA));
            }
        }
    }

    @Override
    public String getDescription() {
        return "Bitte Sie ein Lieblingsgetränk";
    }

    @Override
    public void updateFields() {
        updateCoffeeText();
        updateTeaText();
        
        teaButton.setVisible(MainFrame.user.getFavouriteTea()!= null);
        
        coffeeButton.setVisible(false);
        cappuccinoButton.setVisible(false);
        espressoButton.setVisible(false);
        if(MainFrame.user.getFavouriteCoffee() == null) {
            return;
        }
        switch(MainFrame.user.getFavouriteCoffee().getCoffee()) {
            case BLACK_COFFEE:
                coffeeButton.setVisible(true);
                break;
            case CAPPUCHINO:
                cappuccinoButton.setVisible(true);
                break;
            case ESPRESSO:
                espressoButton.setVisible(true);
                break;
            default:
                throw new AssertionError(MainFrame.user.getFavouriteCoffee().getCoffee().name());
        }
    }
    
    private void updateCoffeeText() {
        DrinkModel.DrinkRepresantation coffee = MainFrame.user.getFavouriteCoffee();
        StringBuilder b = new StringBuilder("<html><center><b>Standardgetränk<br>Kaffee</b></center>");
        if(coffee == null) {
            b.append("<br>Nicht gewählt");
            coffeeText.setText(b.toString());
            return;
        }
            
        b.append("<br><b>Art:</b><br>" + inset);
        switch(coffee.getCoffee()) {
            case BLACK_COFFEE:
                b.append("Kaffee");
                break;
            case CAPPUCHINO:
                b.append("Cappuccino");
                break;
            case ESPRESSO:
                b.append("Espresso");
                break;
            default:
                throw new AssertionError(coffee.getCoffee().name());
        }
        b.append("<br><b>Größe:</b><br>" + inset);
        switch(coffee.getSize()) {
            case SMALL:
                b.append("Klein");
                break;
            case MEDIUM:
                b.append("Mittel");
                break;
            case BIG:
                b.append("Groß");
                break;
            default:
                throw new AssertionError(coffee.getCoffee().name());
        }
        if(coffee.getCoffee() != DrinkModel.Coffee.ESPRESSO) {
            b.append("<br><b>Sorte:</b><br>" + inset);
            switch(coffee.getBean()) {
                case ARABICA:
                    b.append("Arabicabohnen");
                    break;
                case ROBUSTA:
                    b.append("Robustabohnen");
                    break;
                default:
                    throw new AssertionError(coffee.getCoffee().name());
            }
        }
        if(coffee.isCaramel() || coffee.isHazelnut() || coffee.isMilk()) {
            b.append("<br><b>Zusätze:</b>");
            if(coffee.isCaramel())
                b.append("<br>" + inset + "Karamellsirup");
            
            if(coffee.isHazelnut())
                b.append("<br>" + inset + "Haselnusssirup");
            
            if(coffee.isMilk())
                b.append("<br>" + inset + "Milch");
        }
        coffeeText.setText(b.toString());
    }
    
    private void updateTeaText() {
        DrinkModel.DrinkRepresantation tea = MainFrame.user.getFavouriteTea();
        StringBuilder b = new StringBuilder("<html><center><b>Standardgetränk<br>Tee</b></center>");
        if(tea == null) {
            b.append("<br>Nicht gewählt");
            teaText.setText(b.toString());
            return;
        }
            
        b.append("<br><b>Art:</b><br>" + inset);
        switch(tea.getTea()) {
            case GREEN_TEA:
                b.append("Grün");
                break;
            case BLACK_TEA:
                b.append("Schwarz");
                break;
            case FRUIT_TEA:
                b.append("Früchte");
                break;
            default:
                throw new AssertionError(tea.getCoffee().name());
        }
            
        b.append("<br><b>Größe:</b><br>" + inset);
        switch(tea.getSize()) {
            case SMALL:
                b.append("Klein");
                break;
            case MEDIUM:
                b.append("Mittel");
                break;
            case BIG:
                b.append("Groß");
                break;
            default:
                throw new AssertionError(tea.getCoffee().name());
        }
        
             
        b.append("<br><b>Ziehzeit:</b><br>" + inset);
        b.append(tea.getBrewTime());
        b.append(" min, ");
        b.append(tea.getTemperature());
        b.append("°C");
        teaText.setText(b.toString());
    }

    @Override
    public boolean getPreviousButtonEnabled() {
        return true;
    }

    @Override
    public boolean getNextButtonEnabled() {
        return false;
    }

    @Override
    public String getName() {
        return "FAVORITE_NAME";
    }

    @Override
    public GuiPanel getNextPanel() {
        return null;
    }

    @Override
    public GuiPanel getPreviousPanel() {
        return parent.selectDrinkPanel;
    }
}
